package kov.core.body.zy
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	import kov.core.BodyState;
	import kov.core.IBody;
	import kov.core.StateCommand;
	import kov.core.body.StateEnum;
	import kov.core.global.Global;
	import kov.core.global.KeyConfig;
	import kov.events.KeyEvent;
	import kov.ui.KeyCombo;
	import kov.ui.KeyDef;
	
	public class ZYDun extends BodyState
	{
		[Embed("../../../../../assets/zy/dun/0.gif")]
		private var Frame0:Class;
		[Embed("../../../../../assets/zy/dun/1.gif")]
		private var Frame1:Class;
		[Embed("../../../../../assets/zy/dun/2.gif")]
		private var Frame2:Class;
		
		public function ZYDun(body:IBody, priority:int=1)
		{
			super(StateEnum.DUN, body, priority);
			createRenderer();
			createKeyCombo();
		}
		
		// 创建渲染帧。
		private function createRenderer():void
		{
			var f0:BitmapData = (new Frame0() as Bitmap).bitmapData;
			var f1:BitmapData = (new Frame1() as Bitmap).bitmapData;
			var f2:BitmapData = (new Frame2() as Bitmap).bitmapData;
			this.renderer.push(f0);
			this.renderer.push(f1);
			this.renderer.push(f2);
			this.renderer.push(f1); // 第四帧跟第二帧一样的
		}
		
		private var keyStateHash:Dictionary;
		
		// 创建按键。
		private function createKeyCombo():void
		{
			this.keyStateHash = new Dictionary();
			
			// 蹲打
			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([
				KeyDef.create(KeyConfig.A)
			])))] = StateEnum.DUN_DA;
			
			// 跳（恢复站立）
			this.keyStateHash[Global.key.addKeyCombo(new KeyCombo(Vector.<KeyDef>([
				KeyDef.create(KeyConfig.B)
			])))] = StateEnum.XING_ZOU;
		}
		
		override public function execute():void
		{
			this.body.renderer.frameSequence = this.renderer;
			this.body.renderer.delay = 300;
			this.body.renderer.offsetX = 66;
			this.body.renderer.offsetY = 62;
			this.body.renderer.gotoAndStop(0);
			
			Global.stage.addEventListener(Event.ENTER_FRAME, loop);
			Global.key.addEventListener(KeyEvent.WORK, onKeyWork);
		}
		
		override public function exit():void
		{
			Global.stage.removeEventListener(Event.ENTER_FRAME, loop);
			Global.key.removeEventListener(KeyEvent.WORK, onKeyWork);
		}
		
		private function loop(event:Event):void
		{
			var isLeft:Boolean = Global.key.isDown(KeyConfig.LEFT);
			var isRight:Boolean = Global.key.isDown(KeyConfig.RIGHT);
			var isUp:Boolean = Global.key.isDown(KeyConfig.UP);
			var isDown:Boolean = Global.key.isDown(KeyConfig.DOWN);
			
			if ( !(isLeft || isRight || isUp || isDown) ) // 方向键都没有按下
			{
				this.body.renderer.gotoAndStop(0);
				return;
			}
			
			this.body.renderer.play();
			
			var bodyX:Number = this.body.x;
			var bodyY:Number = this.body.y;
			var toX:Number = bodyX;
			var toY:Number = bodyY;
			if (isLeft)
			{
				this.body.faceToLeft = true;
				toX = bodyX - 1;
			}
			else if (isRight)
			{
				this.body.faceToLeft = false;
				toX = bodyX + 1;
			}
			if ( !Global.world.mapData.blockData.isBlock(toX, bodyY) )
			{
				this.body.x = toX;
			}
			
			if (isUp)
			{
				toY = bodyY - 0.5;
			}
			else if (isDown)
			{
				toY = bodyY + 0.5;
			}
			if ( !Global.world.mapData.blockData.isBlock(this.body.x, toY) )
			{
				this.body.y = toY;
			}
		}
		
		// 按键事件。
		private function onKeyWork(event:KeyEvent):void
		{
			var stateName:String = this.keyStateHash[event.keyComboID];
			if (stateName)
			{
				var cmd:StateCommand = new StateCommand(stateName);
				this.body.executeStateCommand(cmd);
			}
		}
		
	}
}